package com.yvan.es;


import com.yvan.metric.Consts;
import com.yvan.metric.Histogram;
import com.yvan.springmvc.IdentityVerification;

import java.util.concurrent.atomic.AtomicLong;

/**
 * ES 调用状况统计
 * Created by luoyifan on 2018/7/21.
 */
public class EsStat {
    private static final AtomicLong executeCount = new AtomicLong();
    private static final AtomicLong reqBytes = new AtomicLong();
    private static final AtomicLong resBytes = new AtomicLong();
    private static final AtomicLong totalCost = new AtomicLong();
    private static long maxReqBytes = 0L;
    private static long maxResBytes = 0L;
    private static long maxCost = 0L;
    private static String maxReqBytesUri = "";
    private static String maxResBytesUri = "";
    private static String maxCostUri = "";
    private static volatile Histogram costHistogram = new Histogram(Consts.ES_COST_HISTOGRAM);
    private static volatile Histogram reqHistogram = new Histogram(Consts.ES_REQ_HISTOGRAM);
    private static volatile Histogram resHistogram = new Histogram(Consts.ES_RES_HISTOGRAM);

    public static void incExecuteCount() {
        executeCount.incrementAndGet();
    }

    public static long getExecuteCount() {
        return executeCount.get();
    }

    public static void resetStat() {
        executeCount.set(0L);
        reqBytes.set(0L);
        resBytes.set(0L);
        totalCost.set(0L);

        costHistogram.reset();
        reqHistogram.reset();
        resHistogram.reset();

        maxReqBytes = 0L;
        maxResBytes = 0L;
        maxCost = 0L;

        maxReqBytesUri = "";
        maxResBytesUri = "";
        maxCostUri = "";
    }

    public static void record(long start, long end, String index, long reqSize, long resSize) {
        long cost = end - start;
        costHistogram.record(cost);

        reqBytes.addAndGet(reqSize);
        resBytes.addAndGet(resSize);

        reqHistogram.record(reqSize);
        resHistogram.record(resSize);

        maxReqBytes = Math.max(reqSize, maxReqBytes);
        maxResBytes = Math.max(resSize, maxResBytes);
        maxCost = Math.max(cost, maxCost);

        if (reqSize == maxReqBytes) {
            if (IdentityVerification.currentRequest() != null) {
                maxReqBytesUri = IdentityVerification.currentRequest().getRequestURI();
            }
        }
        if (resSize == maxResBytes) {
            if (IdentityVerification.currentRequest() != null) {
                maxResBytesUri = IdentityVerification.currentRequest().getRequestURI();
            }
        }
        if (cost == maxCost) {
            if (IdentityVerification.currentRequest() != null) {
                maxCostUri = IdentityVerification.currentRequest().getRequestURI();
            }
        }
        totalCost.addAndGet(cost);
    }

    public static long[] getExecuteHistogram() {
        return costHistogram.toArray();
    }

    public static long[] getResSizeHistogram() {
        return resHistogram.toArray();
    }

    public static long[] getReqSizeHistogram() {
        return reqHistogram.toArray();
    }

    public static long getMaxReqBytes() {
        return maxReqBytes;
    }

    public static long getMaxResBytes() {
        return maxResBytes;
    }

    public static long getMaxCost() {
        return maxCost;
    }

    public static long getTotalCost() {
        return totalCost.get();
    }

    public static long getReqBytes() {
        return reqBytes.get();
    }

    public static long getResBytes() {
        return resBytes.get();
    }

    public static String getMaxReqBytesUri() {
        return maxReqBytesUri;
    }

    public static String getMaxResBytesUri() {
        return maxResBytesUri;
    }

    public static String getMaxCostUri() {
        return maxCostUri;
    }
}
